package org.gikolet.bbs.ui;

import java.util.Hashtable;

import org.gikolet.framework.ui.Container;
import org.gikolet.framework.ui.TabControl;
import org.gikolet.framework.ui.TabPage;
import org.gikolet.framework.view.View;
import org.gikolet.framework.view.ViewLayout;

public class TabbedViewLayout implements ViewLayout {
    private Container container;

    private Hashtable viewTable = new Hashtable();

    private TabControl rootTabControl;

    private TabPage boardsTabPage;
    private TabControl boardsTabControl;

    private TabPage threadHeadersTabPage;
    private TabControl threadHeadersTabControl;

    private TabPage threadsTabPage;
    private TabControl threadsTabControl;

    public TabbedViewLayout() {
	this.rootTabControl = new TabControl();
	this.rootTabControl.setFocusable(false);

	this.boardsTabPage = new TabPage("板一覧");
	this.threadHeadersTabPage = new TabPage("スレッド一覧");
	this.threadsTabPage = new TabPage("スレッド");

	this.rootTabControl.addTabPage(this.boardsTabPage);
	this.rootTabControl.addTabPage(this.threadHeadersTabPage);
	this.rootTabControl.addTabPage(this.threadsTabPage);

	this.boardsTabControl = new TabControl();
	this.boardsTabControl.setFocusable(false);
	this.threadHeadersTabControl = new TabControl();
	this.threadHeadersTabControl.setFocusable(false);
	this.threadsTabControl = new TabControl();
	this.threadsTabControl.setFocusable(false);

	this.boardsTabPage.add(this.boardsTabControl);
	this.threadHeadersTabPage.add(this.threadHeadersTabControl);
	this.threadsTabPage.add(this.threadsTabControl);
    }

    public void setContainer(Container container) {
	this.container = container;
    }

    public void initializeLayout() {
	this.container.add(this.rootTabControl);
    }

    public void addLayoutView(View view) {
	if (this.viewTable.containsKey(view)) {
	    if (view instanceof BoardListView) {
		TabPage tabPage = new TabPage(view.getViewName());
		view.createPartControl(tabPage);

		this.viewTable.put(view, tabPage);

		this.boardsTabControl.addTabPage(tabPage);
		this.boardsTabControl.setTabBarShow(this.boardsTabControl
			.getTabPageSize() >= 2);
	    }
	}
    }

    public void removeLayoutView(View view) {
	Container container = (Container) this.viewTable.remove(view);
	if (view instanceof BoardListView) {
	    if (container != null) {
		this.boardsTabControl.remove(container);
	    }
	}
    }

    public void finalizeLayout() {
	this.viewTable.remove(this.rootTabControl);
    }
}
